import Vue from 'vue'
import Vuex from 'vuex'
import { postLogin } from '@/requests'// 因为是js文件，它无法使用vue上挂载的方法
Vue.use(Vuex)
// rest
let basedata = (store) => {
  store.subscribe((mutation, state) => {
    // 每次 mutation 之后调用
    window.localStorage.setItem('mycart', JSON.stringify(state.cart))
    window.localStorage.setItem('mymsg', JSON.stringify(state.msg))
  })
}
export default new Vuex.Store({
  // 严格模式，避免外部无意修改
  strict: true,
  plugins: [basedata],
  state: {
    cart: JSON.parse(window.localStorage.getItem('mycart')) || [],
    msg: JSON.parse(window.localStorage.getItem('mymsg')) || {name: '', token: ''}

  },
  getters: {

    islogin (state) {
      if (state.msg.token === '') {
        return false
      } else {
        return true
      }
    },
    carlength (state) {
      let sum = 0
      state.cart.forEach((ele) => {
        sum += ele.count
      })
      return sum
    },
    isshow (state) {
      // console.log(state.cart)
      if (state.cart.length === 0) {
        return false
      }
      return true
    },
    totalcount (state) {
      let sum = 0
      state.cart.forEach((ele, index) => {
        sum += ele.count
      })
      return sum
    },
    targetcount (state) {
      let sum = 0
      var arr = state.cart.filter((ele, index) => {
        return ele.checked !== false
      })
      arr.forEach((ele) => {
        sum += ele.count
      })
      return sum
    },
    totalprice (state) {
      let sum = 0
      state.cart.forEach((ele) => {
        sum += ele.price * ele.count
      })
      return sum
    },
    targetprice (state) {
      let sum = 0
      var arr = state.cart.filter((ele, index) => {
        return ele.checked !== false
      })
      arr.forEach((ele) => {
        sum += ele.price * ele.count
      })
      return sum
    },
    isquanxuan (state) {
      let a = true
      if (state.cart.length === 0) {
        a = false
        return a
      }
      for (var i = 0; i < state.cart.length; i++) {
        // console.log(state.cart[i].checked)
        if (!state.cart[i].checked) {
          a = false
        }
      }
      // console.log(a)
      return a
    }
  },
  mutations: {
    headback (state) {
      state.msg = {name: '', token: ''}
    },
    del (state, id) {
      // console.log(state === this.state) true
      state.cart = state.cart.filter(ele => {
        return ele.id !== id
      })
    },
    dan (state, id) {
      state.cart.map((ele) => {
        if (ele.id === id) {
          ele.checked = !ele.checked
        }
        return ele
      })
    },
    quan (state, e) {
      let ischecked = e.target.checked
      state.cart.forEach((ele) => {
        ele.checked = ischecked
      })
    },
    myadd (state, obj) {
      let has = state.cart.some((ele) => {
        return ele.id === obj.id
      })
      if (has) {
        state.cart.forEach((ele) => {
          if (ele.id === obj.id) {
            ele.count++
          }
        })
      } else {
        let myobj = {
          ...obj,
          checked: false,
          count: 1
        }
        state.cart.push(myobj)
      }
    },
    myjia (state, id) {
      state.cart.forEach((ele) => {
        if (ele.id === id) {
          ele.count++
        }
      })
    },
    myjian (state, id) {
      state.cart.forEach((ele) => {
        if (ele.id === id) {
          if (ele.count !== 0) {
            ele.count--
          }
        }
      })
    },
    transmsg (state, obj) {
      state.msg = obj
      // console.log(state.msg)
    }
  },
  // 尝试一下异步操作state
  actions: {
    asydel (store, id) {
      setTimeout(() => {
        store.commit('del', id)
        // 实质还是调用本来的方法
      }, 2000)
    },
    ajaxtransmsg (store, obj) {
      postLogin(obj).then(res => {
        console.log(res)
        if (res.data.code === '200') {
          console.log(200 + '?')
          store.commit('transmsg', res.data.data)
        }
      })
    }
  }

})
